package cn.edu.ctbu.labtest.service.impl;

import cn.edu.ctbu.labtest.dao.StudentRepository;
import cn.edu.ctbu.labtest.domain.Student;
import cn.edu.ctbu.labtest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentimplService implements StudentService {
    @Autowired
    StudentRepository studentRepository;
    /**
     * 读取所有学生
     * @return
     */
    public List<Student> findAll(){
        return studentRepository.findAll();

    }

    /*
     * 只分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    };


    /*
     *分页,带过滤的查询
     *param pageable
     *@return
     */
    public Page<Student> findAll(Example<Student> student, Pageable pageable){
        return studentRepository.findAll(student, pageable);
    };



    /**
     *插入学生
     */
    public Student insert(Student student){

        return studentRepository.save(student);
    }
    /**
     * 更新学生
     */
    public Student update(Student student){

        return  studentRepository.save(student);
    }

    public Student getById(Long id){

        Student student=studentRepository.findById(id).orElse(null);

        return student;
    }
    public void delete(Student student){

        studentRepository.delete(student);

    }

    public void delete(Long id){
        Student student=new Student();
        student.setId(id);

        studentRepository.delete(student);
    }

    /**
     *删除学生
     */
    public void del(Long id){

        studentRepository.deleteById(id);
    }
    
    
    /**
     * 查找学生
    public Student search(Long id)
    {
        return studentRepository.findById(id).get();
    }
    public List<Student> findStudentByName(String name) { return studentRepository.findStudentByName(name);}
    */

}
